package com.fgr.controller;

import com.fgr.beans.Adapter;
import com.fgr.beans.Animal;
import com.fgr.beans.User;
import com.fgr.service.AdapterService;
import com.fgr.service.AnimalService;
import com.fgr.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Controller
public class AdapterController {
    @Autowired
    AdapterService adapterService;

    @Autowired
    UserService userService;

    @Autowired
    AnimalService animalService;

    @GetMapping("selectAllAdapterAndUser")
    public String selectAllAdapterAndUser(Integer publish,Model model){
        List<Adapter> adapters = adapterService.selectAllAdapterAndUser(publish);
        model.addAttribute("adapters",adapters);
        if (publish==1){
            return "background/adapter";
        }else {
            return "background/auditing-adapter";
        }
    }

    @PostMapping("addAdapterBg")
    @ResponseBody
    public String addAdapterBg(Adapter adapter){
        String s = adapterService.addAdapterBg(adapter);
        return s;

    }

    @ResponseBody
    @PostMapping("addAdapterAuditing")
    public String addAdapterAuditing(Integer anId, HttpSession httpSession){
        String s = adapterService.insertAdapterAuditing(anId, httpSession);
        return s;
    }

    @PostMapping("deleteAdapterByIdBg")
    @ResponseBody
    public String deleteAdapterByIdBg(Integer adId){
        String s = adapterService.deleteAdapter(adId);
        return s;
    }

    @GetMapping("selectUserAndAdapterAndAnimal")
    public String selectUserAndAdapterAndAnimal(Integer adId,Model model){
        List<User> users = userService.selectAllUser();
        List<Animal> animals = animalService.selectAllAnimal();
        Adapter adapter = adapterService.selectAdapterByAdId(adId);

        model.addAttribute("users",users);
        model.addAttribute("animals",animals);
        model.addAttribute("adapter",adapter);

        return "background/modifyadapter";
    }

    @GetMapping("selectAdapterAndUserByAdIdAuditing")
    public String selectAdapterAndUserByAdIdAuditing(Integer adId,Model model){
        Adapter adapter = adapterService.selectAdapterAndUserByAdIdAuditing(adId);
        model.addAttribute("adapter",adapter);
        return "background/auditadapter";
    }


    @PostMapping("UpdateAdapterByAdId")
    @ResponseBody
    public String UpdateAdapterByAdId(Adapter adapter){
        String s = adapterService.updateAdapter(adapter);
        return s;

    }

    @ResponseBody
    @PostMapping("updateAdapterPublishByAdId")
    public String updateAdapterPublishByAdId(Integer adId,Integer anId){
        //将申请领养状态修改为已通过
        String s = adapterService.updateAdapterPublishByAdId(adId);
        //将动物状态修改为已经领养
        String s1 = animalService.updateAnimalStat(anId);
        System.out.println(s1);
        if (s.equals(s1)){
            return s;
        }else {
            return "ERRO";
        }

    }




}
